#ifndef BS_WS_H
#define BS_WS_H

#include <drogon/HttpAppFramework.h>
#include <drogon/PubSubService.h>
#include <drogon/WebSocketController.h>

using namespace drogon;

class BsWebSocket : public drogon::WebSocketController<BsWebSocket> {
public:
	BsWebSocket() {
		// 启动全局定时器
		HttpAppFramework::instance().getLoop()->runEvery(10.0, [this] {
			sendHeartbeatToAll("dev");
		});
	}

	~BsWebSocket() {
		LOG_INFO << "~BsWebSocket";
	}

	void handleNewMessage(const WebSocketConnectionPtr &,
			std::string &&,
			const WebSocketMessageType &) override;

	void handleNewConnection(const HttpRequestPtr &,
			const WebSocketConnectionPtr &) override;

	void handleConnectionClosed(const WebSocketConnectionPtr &) override;

	WS_PATH_LIST_BEGIN
	WS_PATH_ADD("/bsws");
	WS_PATH_LIST_END

	void sendMessageToAll(const std::string &room, const std::string &message) const;

private:
	PubSubService<std::string> wsRooms;
	void sendHeartbeatToAll(const std::string &room) const;
};

struct BsClient {
	std::string roomId;
	std::string userId;
	drogon::SubscriberID bsId;
};

#endif // BS_WS_H